package GUI;

import javax.swing.JPanel;
import java.awt.SystemColor;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.Color;
import javax.swing.JButton;

import Client.BusinessImplements;
import Controler.Delete_Autho_controler;
import Controler.Update_Author_Controler;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;

import javax.swing.JComboBox;
import javax.swing.border.LineBorder;
/**
  * @author Lucie
 * Class name Delete_Autho_Panel
 * extend  Abstract_Jpanel
 * purpose: this panel get name of author from the user and call the controller 
 * to delete this  author
 */
public class Delete_Autho_Panel extends  Abstract_Jpanel {

	private JComboBox comboBox; 
	private String[] all_authors_name;
	private String[] all_authors_id;
	private ArrayList<String> all_authors;
	/**
	 * Create the panel.
	 */
	public Delete_Autho_Panel() {
		setLayout(null);
		
		JPanel panel = new JPanel();
		panel.setBorder(new LineBorder(SystemColor.activeCaption, 6));
		panel.setBackground(SystemColor.inactiveCaption);
		panel.setBounds(47, 59, 461, 233);
		add(panel);
		panel.setLayout(null);
		
		JLabel lblAuthorName = new JLabel("author name:");
		lblAuthorName.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblAuthorName.setBounds(47, 68, 88, 17);
		panel.add(lblAuthorName);
		
		 comboBox = new JComboBox();
		comboBox.setBounds(161, 68, 131, 20);
		panel.add(comboBox);
		
		JPanel panel_1 = new JPanel();
		panel_1.setBorder(new LineBorder(SystemColor.control, 6));
		panel_1.setBackground(new Color(220, 220, 220));
		panel_1.setBounds(20, 35, 384, 150);
		panel.add(panel_1);
		panel_1.setLayout(null);
		
		JLabel lblChooseAuthorTo = new JLabel("choose author to delete");
		lblChooseAuthorTo.setForeground(new Color(25, 25, 112));
		lblChooseAuthorTo.setFont(new Font("MV Boli", Font.BOLD, 30));
		lblChooseAuthorTo.setBounds(58, 11, 413, 37);
		add(lblChooseAuthorTo);
		/*
		 * btnBack ;When you click this button you will return to 
		 *	sing in panel screen
		 */
		JButton btnBack = new JButton("back");
		btnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Sign_in_panel sip=new Sign_in_panel();
				on_screen_unload();
				BusinessImplements.GetInstance().getMainFrame().setContentPane(sip);
				sip.setSize(getMaximumSize());
				sip.on_screen_load();

				
			}
		});
		btnBack.setFont(new Font("Tahoma", Font.PLAIN, 14));
		btnBack.setBounds(71, 313, 97, 37);
		add(btnBack);
		
		/*
		 *btnGo=When you click this button you will call delete_author(get_autor_id())
		 * a method of the controller of this panel if this method return true we will
		 * call the methods of this class next_screen_success(),empty_text_fields();
		 * else we call next_screen_faild(delete_author_controler.get_msg());	
		 * 	delete_author_controler.get_msg(); return the msg from the controller with all
		 * the error msg.
		 */
		JButton btnGo = new JButton("go");
		btnGo.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			
				if (!get_autor_id().equals(""))
				{
					Delete_Autho_controler delete_author_controler=new Delete_Autho_controler();
					
					if(delete_author_controler.delete_author( get_autor_id()))
					{
						next_screen_success();
						empty_text_fields();
						
					}
					else {
						next_screen_faild(delete_author_controler.get_msg());
						
					}
		
					
					
				}
				else
					error_msg();
				
				
			}
		});
		btnGo.setFont(new Font("Tahoma", Font.PLAIN, 14));
		btnGo.setBounds(411, 313, 97, 37);
		add(btnGo);

	}

	@Override
	public void next_screen() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void prev_screen() {
		// TODO Auto-generated method stub
		
	}

	@Override
	/**
	 * this method is the first method we call in this screen
	 * the main purpose of this method is to initialize the comboBox 
	 * that this panel use, here we call the controller of this panel
	 * to get all author names and id's and set them at the combobox
	 */
	public void on_screen_load() {

		
		Update_Author_Controler update_author_controler=new  Update_Author_Controler();
		all_authors=update_author_controler.get_all_authors();
		
		String[] line;
		all_authors_name = new String[all_authors.size()+1];
		all_authors_id = new String[all_authors.size()+1];
		all_authors_name[0] = "";
		all_authors_id[0] =  "";
		for (int i = 1 ; i <= all_authors.size() ; i++)
		{
			line = all_authors.get(i-1).split(",");
			all_authors_id[i] = line[0];
			all_authors_name[i] = line[1];
			
		}
		
		comboBox.setModel(new DefaultComboBoxModel(all_authors_name));
		comboBox.setBounds(132, 32, 121, 20);
		
		
	}

	@Override
	public void on_screen_unload() {
		// TODO Auto-generated method stub
		
	}
	/**
	 * 
	 * @return author id of the author that was chosen in the combobox
	 */
	public String get_autor_id()
	{
		
		if (!comboBox.getSelectedItem().equals(""))
			{
				for(int i=0;i< all_authors_name.length;i++)
					if( all_authors_name[i].equals(comboBox.getSelectedItem()))
						
						return all_authors_id[i];
				   	
			
			}
		
		return "";
	}
	/**
	 *  this method display showMessageDialog with a failed msg
	 */
	public void error_msg()
	{
	JOptionPane.showMessageDialog(this,"miss author deatails ");
	}
	/**
	 * this method display showMessageDialog with a success msg
	 */
	public void next_screen_success()
	{
		JOptionPane.showMessageDialog(this,"delete success");
	}
	/**
	 *   this method display showMessageDialog with a failed msg
	 * @param msg
	 */
	public void next_screen_faild(String msg)
	{
		JOptionPane.showMessageDialog(this,"delete faild "+msg);
	}
	/**
	 * this method initially the panel
	 */
	public void empty_text_fields()
	{
		on_screen_load();
		
	}
}
